package com.sense.kukoo.biz_explore.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_explore.model.Content2Model
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.Repo
import com.sense.kukoo.common.repo.biz.ExploreContentBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.repo.http.bean.ModuleBean
import com.sense.kukoo.lib_logger.LoggerManager

class Content2ViewModel: BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private var mPageIndex = 2
    private val mPageSize = 20
    private val mData = arrayListOf<IndexContentBean>()
    private var mFirstId = -1L

    private var errorRetry: ()->Unit = {}

    private lateinit var mModuleBean: ModuleBean

    val action_finishRefresh: MutableLiveData<Boolean> = MutableLiveData()
    val action_finishLoadMore: MutableLiveData<Boolean> = MutableLiveData()
    val action_networkError: MutableLiveData<Boolean> = MutableLiveData()
    val data_content:MutableLiveData<List<ExploreContentBean>> = MutableLiveData()
    val data_enableLoadMore:MutableLiveData<Boolean> = MutableLiveData()

    override fun createModel(): BaseMvvmModel? = null

    override fun initData() {}

    fun setData(bean: ModuleBean){
        mModuleBean = bean
    }

    fun refreshData(){
        getIndexContents(true)
    }

    fun loadMoreData(){
        getIndexContents(false)
    }

    fun getIndexContents(isRefresh :Boolean){
        val onError: (error: Throwable) -> Unit = {
            it.printStackTrace()
            errorRetry = {getIndexContents(isRefresh)}
            action_networkError.postValue(true)
        }
        request(onError) {
            val pageIndex: Int
            if (mModuleBean.isRecommend()) {
                pageIndex = 1
            } else {
                if (isRefresh) {
                    pageIndex = 1
                } else {
                    pageIndex = mPageIndex
                }
            }
            val result = Repo.http.indexContent(pageIndex, mPageSize, mModuleBean.id)
            if (result.success){
                val data:List<IndexContentBean> = result.data!!
                //去重
                if(mModuleBean.isRecommend() && data.size > 0){
                    val firstId:Long = data.first().id ?: 0L
                    if(mFirstId == firstId){
                        //重复的数据，忽略，并重试
                        LoggerManager.e(TAG, "重复的数据，重试")
                        Thread.sleep(100)
                        getIndexContents(isRefresh)
                        return@request
                    } else {
                        mFirstId = firstId
                    }
                }
                //
                if (isRefresh) {
                    mPageIndex = 2
                    mData.clear()
                } else {
                    mPageIndex++
                }
                mData.addAll(data)
                //make data
                val exploreContent = arrayListOf<ExploreContentBean>()
                exploreContent.add(ExploreContentBean(type = ExploreContentBean.TYPE_HEADER, null))
                mData.forEach{
                    exploreContent.add(ExploreContentBean(data = it))
                }
                //show data
                if(data.size < mPageSize){
                    //没有更多数据了
                    data_enableLoadMore.postValue(false)
                    exploreContent.add(ExploreContentBean(type = ExploreContentBean.TYPE_FOOTER, null))
                } else {
                    data_enableLoadMore.postValue(true)
                }
                data_content.postValue(exploreContent)
                action_finishRefresh.postValue(true)
                action_finishLoadMore.postValue(true)
            } else {
                Log.e(TAG, "result0=" + result.msg)
                errorRetry = {getIndexContents(isRefresh)}
                action_networkError.postValue(true)
            }
        }
    }

    fun onErrorRetry(){
        errorRetry()
    }
}